﻿using System.IO;
using System.Windows.Forms;
using ClickOnceMaster.Common;
using ClickOnceMaster.Properties;
using ClickOnceMaster.WizardConfiguration;
using System;

namespace ClickOnceMaster.Pages
{
    public partial class FilesPage : WizardPage
    {
        #region .ctors

        public FilesPage()
        {
            InitializeComponent();
            Text = Resources.FilesPage;
        }

        #endregion

        #region Instance methods

        #region WizardPage overrides

        protected override void LoadData()
        {
            base.LoadData();
            if (WizardContext.SourceDirectory != null)
            {
                FileInfo[] files = WizardContext.SourceDirectory.GetFiles();

                foreach (var info in files)
                {
                    ManifestFile file = new ManifestFile(info);

                    if (info.Extension == ".exe")
                    {
                        file.FileType = Common.FileType.EntryPoint;
                    }

                    if (info.Extension == ".config")
                    {
                        file.FileType = Common.FileType.Config;
                    }

                    WizardContext.Files.Add(file);
                }
            }
        }

        protected override void BindPage()
        {
            base.BindPage();
            ((DataGridViewComboBoxColumn)gvFiles.Columns[1]).DataSource = Enum.GetValues(typeof(FileType));
            bsFiles.DataSource = WizardContext.Files;
        }

        #endregion

        #region Evenat handlers

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            AddFiles();
        }

        #endregion

        #region Auxiliary methods

        /// <summary>
        /// Adds custom file to manifest.
        /// </summary>
        private void AddFiles()
        {
            // Prompt to select files to move into application
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.Title = Resources.AddAppFiles;
            ofd.Filter = string.Format("{0} (*.*)|*.*", Resources.AllFiles);

            // Remember last path used to select files
            if (!string.IsNullOrEmpty(WizardSettings.Current.Manifest.FilesPath) &&
                (Directory.Exists(WizardSettings.Current.Manifest.FilesPath)))
            {
                ofd.InitialDirectory = WizardSettings.Current.Manifest.FilesPath;
            }

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //Save selected path
                WizardSettings.Current.Manifest.FilesPath = Path.GetDirectoryName(ofd.FileName);
                //Prompt for destination folder and add files
                string[] fileNames = ofd.FileNames;               

                foreach (string fileName in fileNames)
                {
                    bsFiles.Add(new ManifestFile(fileName));
                }
            }
        }

        #endregion

        #endregion       
    }
}